<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-content">
        <div class="logo">油柑博客</div>
        <nav class="nav">
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link v-for="category in categories" :key="category.id" 
            :to="{ name: 'Category', params: { id: category.id } }" 
            class="nav-item">
            {{ category.name }}
          </router-link>
        </nav>
        <div class="user-actions">
          <router-link to="/login" class="login-btn" v-if="!userInfo.token">登录</router-link>
          <div class="user-info" v-else>
            <span>{{ userInfo.nickname || userInfo.username }}</span>
            <el-dropdown trigger="click">
              <span class="avatar">
                <img :src="userInfo.avatar || 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'" alt="头像">
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item v-if="userInfo.roleId === 1">
                    <router-link to="/admin/articles">后台管理</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </header>

    <!-- 页面主体 -->
    <div class="main-content">
      <el-row :gutter="20">
        <!-- 文章列表区域 -->
        <el-col :xs="24" :sm="24" :md="18">
          <div class="articles">
            <div class="welcome-banner" v-if="articles.length > 0">
              <h1>欢迎来到油柑博客</h1>
              <p>这里记录着编程、生活和思考</p>
            </div>

            <div v-if="articles.length === 0" class="no-articles">
              <el-empty description="暂无文章"></el-empty>
            </div>

            <div v-else class="article-list">
              <div v-for="article in articles" :key="article.id" class="article-card">
                <div class="article-meta">
                  <span class="category">{{ article.categoryName }}</span>
                  <span class="date">{{ formatDate(article.createTime) }}</span>
                </div>
                <h2 class="article-title">
                  <router-link :to="{ name: 'Article', params: { id: article.id } }">
                    {{ article.title }}
                  </router-link>
                </h2>
                <div class="article-summary">{{ article.summary }}</div>
                <div class="article-footer">
                  <span class="author">{{ article.authorName || '未知作者' }}</span>
                  <span class="views"><el-icon><View /></el-icon> {{ article.viewCount }}</span>
                  <router-link :to="{ name: 'Article', params: { id: article.id } }" class="read-more">
                    阅读全文
                  </router-link>
                </div>
              </div>
            </div>
          </div>
        </el-col>

        <!-- 侧边栏区域 -->
        <el-col :xs="24" :sm="24" :md="6">
          <div class="sidebar">
            <!-- 站点信息 -->
            <div class="sidebar-card site-info">
              <div class="avatar">
                <img src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" alt="站点头像">
              </div>
              <h3>油柑博客</h3>
              <p>记录编程知识与生活感悟</p>
              <div class="site-stats">
                <div class="stat-item">
                  <div class="count">{{ articles.length }}</div>
                  <div class="label">文章</div>
                </div>
                <div class="stat-item">
                  <div class="count">{{ categories.length }}</div>
                  <div class="label">分类</div>
                </div>
              </div>
            </div>

            <!-- 分类列表 -->
            <div class="sidebar-card categories">
              <h3 class="card-title">分类</h3>
              <ul class="category-list">
                <li v-for="category in categories" :key="category.id" class="category-item">
                  <router-link :to="{ name: 'Category', params: { id: category.id } }">
                    <span class="category-name">{{ category.name }}</span>
                    <span class="category-count">5</span> <!-- 理想情况下应该显示每个分类下的文章数 -->
                  </router-link>
                </li>
              </ul>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
      <div class="footer-content">
        <p>&copy; {{ new Date().getFullYear() }} 油柑博客 - 保留所有权利</p>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { View } from '@element-plus/icons-vue'
import { getArticleList } from '@/api/article'
import { getCategoryList } from '@/api/category'

const router = useRouter()
const articles = ref([])
const categories = ref([])
const userInfo = ref({})

const fetchArticles = async () => {
  try {
    const res = await getArticleList()
    articles.value = res.data
  } catch (error) {
    console.error('获取文章列表失败:', error)
  }
}

const fetchCategories = async () => {
  try {
    const res = await getCategoryList()
    categories.value = res.data
  } catch (error) {
    console.error('获取分类列表失败:', error)
  }
}

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  const options = { year: 'numeric', month: 'long', day: 'numeric' }
  return date.toLocaleDateString('zh-CN', options)
}

const handleLogout = () => {
  localStorage.removeItem('user')
  userInfo.value = {}
  ElMessage.success('退出登录成功')
  router.push('/')
}

onMounted(() => {
  fetchArticles()
  fetchCategories()
  userInfo.value = JSON.parse(localStorage.getItem('user') || '{}')
})
</script>

<style scoped>
/* 整体布局 */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 顶部导航栏 */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}

.nav {
  display: flex;
  gap: 20px;
}

.nav-item {
  color: #606266;
  text-decoration: none;
  font-weight: 500;
  padding: 8px 0;
  position: relative;
}

.nav-item:hover, .nav-item.router-link-active {
  color: #409EFF;
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #409EFF;
  transition: width 0.3s;
}

.nav-item:hover::after, .nav-item.router-link-active::after {
  width: 100%;
}

.user-actions {
  display: flex;
  align-items: center;
}

.login-btn {
  background-color: #409EFF;
  color: white;
  padding: 8px 16px;
  border-radius: 4px;
  text-decoration: none;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #66b1ff;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 主体内容 */
.main-content {
  flex: 1;
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

/* 欢迎横幅 */
.welcome-banner {
  text-align: center;
  padding: 40px 0;
  margin-bottom: 40px;
  background: linear-gradient(to right, #f6f9fe, #eef5fe);
  border-radius: 8px;
}

.welcome-banner h1 {
  font-size: 32px;
  margin-bottom: 10px;
  color: #303133;
}

.welcome-banner p {
  font-size: 16px;
  color: #606266;
}

/* 文章列表 */
.article-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-card {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
}

.article-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.article-meta {
  display: flex;
  gap: 15px;
  margin-bottom: 10px;
}

.category {
  display: inline-block;
  background-color: #ecf5ff;
  color: #409EFF;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.date {
  color: #909399;
  font-size: 12px;
}

.article-title {
  font-size: 20px;
  margin: 8px 0;
}

.article-title a {
  color: #303133;
  text-decoration: none;
  transition: color 0.3s;
}

.article-title a:hover {
  color: #409EFF;
}

.article-summary {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
  margin-bottom: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #909399;
  font-size: 13px;
}

.author {
  display: flex;
  align-items: center;
  gap: 5px;
}

.views {
  display: flex;
  align-items: center;
  gap: 5px;
}

.read-more {
  color: #409EFF;
  text-decoration: none;
  font-size: 13px;
}

/* 侧边栏 */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: sticky;
  top: 80px;
}

.sidebar-card {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.site-info {
  text-align: center;
}

.site-info .avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  margin: 0 auto 15px;
}

.site-info h3 {
  font-size: 18px;
  margin-bottom: 5px;
}

.site-info p {
  color: #606266;
  font-size: 14px;
  margin-bottom: 15px;
}

.site-stats {
  display: flex;
  justify-content: space-around;
  margin-top: 15px;
}

.stat-item {
  text-align: center;
}

.stat-item .count {
  font-size: 20px;
  font-weight: bold;
  color: #409EFF;
}

.stat-item .label {
  font-size: 13px;
  color: #909399;
}

.card-title {
  font-size: 16px;
  margin-bottom: 15px;
  color: #303133;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 10px;
}

.category-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.category-item {
  margin-bottom: 10px;
}

.category-item a {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  color: #606266;
  text-decoration: none;
  transition: color 0.3s;
}

.category-item a:hover {
  color: #409EFF;
}

.category-count {
  background-color: #f0f2f5;
  color: #909399;
  font-size: 12px;
  border-radius: 10px;
  padding: 0 8px;
  min-width: 20px;
  text-align: center;
}

/* 页脚 */
.footer {
  background-color: #f4f6f9;
  padding: 20px;
  text-align: center;
  margin-top: 40px;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  color: #909399;
  font-size: 14px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    height: auto;
    padding: 10px 20px;
  }

  .nav {
    margin: 10px 0;
    flex-wrap: wrap;
    justify-content: center;
  }

  .user-actions {
    margin-top: 10px;
  }

  .welcome-banner {
    padding: 20px 0;
  }

  .welcome-banner h1 {
    font-size: 24px;
  }

  .sidebar {
    margin-top: 20px;
    position: static;
  }
}
</style>